package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.StatusCategoryEnum;
import cn.huiyunche.base.service.interfaces.CWaybillService;
import cn.huiyunche.base.service.interfaces.LogisticsInfoService;
import cn.huiyunche.base.service.interfaces.SOrderTrailService;
import cn.huiyunche.base.service.interfaces.WaybillTrailService;
import cn.huiyunche.base.service.mappers.CWaybillBidMapper;
import cn.huiyunche.base.service.mappers.SOrderStatusLogMapper;
import cn.huiyunche.base.service.model.CWaybill;
import cn.huiyunche.base.service.model.SOrderStatusLog;
import cn.huiyunche.base.service.model.SOrderStatusLogExample;
import cn.huiyunche.base.service.utils.DateUtils;
import cn.huiyunche.base.service.vo.LogisticsInfoVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class LogisticsInfoServiceImpl implements LogisticsInfoService {

    private static final Logger logger = LoggerFactory.getLogger(LogisticsInfoServiceImpl.class);

    @Autowired
    private SOrderStatusLogMapper sOrderStatusLogMapper = null;

    @Autowired
    private CWaybillService waybillService = null;

    @Autowired
    private SOrderTrailService sorderTrailService = null;

    @Autowired
    private WaybillTrailService waybillTrailService = null;

    /**
     * 竞单记录表 Mapper
     */
    @Autowired
    private CWaybillBidMapper cWaybillBidMapper = null;

    @Override
    public LogisticsInfoVo listByWaybill(Long waybillId) {
        if (null == waybillId) {
            logger.info("waybillId is not null");
            throw new BusinessException("运单ID不能为空");
        }
        CWaybill waybill = waybillService.getWayBillById(waybillId);
        if (waybill == null) {
            logger.info("waybill is null");
            throw new BusinessException("运单信息为空");
        }
        LogisticsInfoVo vo = new LogisticsInfoVo();
        List<Integer> status = new ArrayList<Integer>();

        SOrderStatusLogExample example = new SOrderStatusLogExample();
        example.setOrderByClause("create_time desc");
        example.createCriteria().andOrderIdEqualTo(waybill.getOrderId())
                .andCategoryEqualTo(StatusCategoryEnum.PROCESS.getValue()).andStatusIn(status);
        List<SOrderStatusLog> sLogs = sOrderStatusLogMapper.selectByExample(example);
        List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
        Map<String, String> map = null;
        if (CollectionUtils.isNotEmpty(sLogs) && sLogs.size() > 0) {
            for (SOrderStatusLog log : sLogs) {
                map = new HashMap<String, String>();
                String time = DateUtils.getStringFromDate(log.getCreateTime(), "yyyy-MM-dd HH:mm");
                map.put("date", time);
                lists.add(map);
            }
            vo.setMessage(lists);
        }
        logger.info(vo.toString());
        return vo;
    }

    public CWaybillBidMapper getcWaybillBidMapper() {
        return this.cWaybillBidMapper;
    }

    public SOrderTrailService getSorderTrailService() {
        return sorderTrailService;
    }

    public WaybillTrailService getWaybillTrailService() {
        return waybillTrailService;
    }
}
